//
//  NetworkTools.swift
//  SwiftPhophotoBrowser
//
//  Created by xmg on 16/8/20.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType{
    case GET
    case POST
}

class NetworkTools: AFHTTPSessionManager {

    /** 设置单列对象 */
    static let shareInstance : NetworkTools = {
        let tools = NetworkTools();
        tools.responseSerializer.acceptableContentTypes?.insert("text/html");
        return tools
    }()
    
}

extension NetworkTools {

    /** 封装请求 */
    func request(type :RequestType,urlString :String,parameters : [String : NSObject],callback : (progress : NSProgress?,result : AnyObject? , error : NSError?)->()){
    
        /** 定义闭包 */
        let progressCallBack = { (progress:NSProgress) -> Void in
            callback(progress: progress, result: nil, error: nil)
        }
        let successCallBack = { (task :NSURLSessionDataTask, result:AnyObject?) -> Void in
            callback(progress: nil, result: result, error: nil)
        }
        let errorCallBack = { (task :NSURLSessionDataTask?, error :NSError) -> Void in
            callback(progress: nil, result: nil, error: error)
        }
        
        if type == .GET {
            GET(urlString, parameters: parameters, progress: progressCallBack, success: successCallBack, failure: errorCallBack)
        }else{
           POST(urlString, parameters: parameters, progress: progressCallBack, success: successCallBack, failure: errorCallBack)
            
        }
       
    }
}